package jakey.SalaryCount;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

@SuppressWarnings("deprecation")
public class SalaryCountActivity extends Activity {
    /** Called when the activity is first created. */
	private TabHost tabHost;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        
        Resources res = getResources();
        tabHost = (TabHost)findViewById(R.id.tabhost);
        TabHost.TabSpec spec;
        
        LocalActivityManager groupActivity = new LocalActivityManager(this,false);
        groupActivity.dispatchCreate(savedInstanceState);
        tabHost.setup(groupActivity);
        
        Intent intent; 
        
        intent = new Intent().setClass(this, HomeActivity.class);       
        spec = tabHost.newTabSpec("home").setIndicator("首页",
                          res.getDrawable(R.drawable.ic_tab_home))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, AddTeacherActivity.class);       
        spec = tabHost.newTabSpec("add").setIndicator("添加",
                          res.getDrawable(R.drawable.ic_tab_addteacher))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, BaseDataActivity.class);
        spec = tabHost.newTabSpec("basedata").setIndicator("课时费",
                          res.getDrawable(R.drawable.ic_tab_baseinfo))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
        
        //tab切换时刷新
        tabHost.setOnTabChangedListener(new OnTabChangeListener(){

			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if(tabId.equals("home")) {
					HomeActivity.self.refreshListData();
				}
			}       	     	
        });
		
    }

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		HomeActivity.self.refreshListData();
	}

	@Override
	//返回按键菜单
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, 1, 1, "退出");
		menu.add(0, 2, 2, "最小化");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	//菜单触发逻辑
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getItemId() == 1) {
			SalaryCountActivity.this.finish();
		}else if(item.getItemId() == 2) {
			goHome(SalaryCountActivity.this);
		}
		return super.onOptionsItemSelected(item);
	}

	//最小化
	public static void goHome(Activity activity){
		Intent intent = new Intent();
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.HOME");
		activity.startActivity(intent);
	}
	
}
